<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-06-28 14:49:32
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\BaseData;

use App\Http\Services\Service;
use App\Repositories\DefectDetailsRepository;
use App\Repositories\DefectRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property DefectDetailsRepository $detail
 * @property DefectRepository $repository
 */
class DefectService extends Service
    {

    public function __construct(DefectRepository $repository, DefectDetailsRepository $detail)
        {
        $this->repository = $repository;
        $this->detail     = $detail;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    protected function save($request)
        {
        // DB::enableQueryLog();
        // dd(DB::getQueryLog());
        $data = $request->toArray();
        $id   = is_numeric($data['id'] ?? "") ? $data['id'] : 0;
        if ($id === 0) {
            $gdata = $this->repository->whereDefectname($data['defectName'])->first();
            if ($gdata) {
                fail("改疵点已存在！不允许疵点重复！");
                }
            }
        return $this->repository->autosave($data);
        }
    protected function delete()
        {
        return $this->repository->with(['details'])->autodelete($this->getRequestIds());
        }
    public function details()
        {
        return $this->detail->condition(request())->getGrid();
        }
    }